package com.ruoyi.gczl.clzxzl.cgbg.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gczl.clzxzl.cgbg.domain.ClzxzlCgbg;
import com.ruoyi.gczl.clzxzl.cgbg.service.IClzxzlCgbgService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 测量专项资料成果报告Controller
 *
 * @author huacai
 * @date 2024-08-26
 */
@RestController
@RequestMapping("/clzxzl/cgbg")
public class ClzxzlCgbgController extends BaseController {
    @Resource
    private IClzxzlCgbgService clzxzlCgbgService;

    /**
     * 查询测量专项资料成果报告列表
     */
    @PreAuthorize("@ss.hasPermi('clzxzl:cgbg:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClzxzlCgbg clzxzlCgbg) {
        startPage();
        List<ClzxzlCgbg> list = clzxzlCgbgService.selectClzxzlCgbgList(clzxzlCgbg);
        return getDataTable(list);
    }

    /**
     * 导出测量专项资料成果报告列表
     */
    @PreAuthorize("@ss.hasPermi('clzxzl:cgbg:export')")
    @Log(title = "测量专项资料成果报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClzxzlCgbg clzxzlCgbg) {
        List<ClzxzlCgbg> list = clzxzlCgbgService.selectClzxzlCgbgList(clzxzlCgbg);
        ExcelUtil<ClzxzlCgbg> util = new ExcelUtil<ClzxzlCgbg>(ClzxzlCgbg.class);
        util.exportExcel(response, list, "测量专项资料成果报告数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ClzxzlCgbg> util = new ExcelUtil<ClzxzlCgbg>(ClzxzlCgbg.class);
        util.importTemplateExcel(response, "测量专项资料成果报告数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "测量专项资料成果报告", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('clzxzl:cgbg:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ClzxzlCgbg> util = new ExcelUtil<ClzxzlCgbg>(ClzxzlCgbg.class);
        InputStream inputStream = file.getInputStream();
        List<ClzxzlCgbg> list = util.importExcel(inputStream);
        inputStream.close();
        int count = clzxzlCgbgService.batchInsertClzxzlCgbg(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取测量专项资料成果报告详细信息
     */
    @PreAuthorize("@ss.hasPermi('clzxzl:cgbg:query')")
    @GetMapping(value = "/{clzxzlCgbgId}")
    public AjaxResult getInfo(@PathVariable("clzxzlCgbgId") Integer clzxzlCgbgId) {
        return success(clzxzlCgbgService.selectClzxzlCgbgByClzxzlCgbgId(clzxzlCgbgId));
    }

    /**
     * 新增测量专项资料成果报告
     */
    @PreAuthorize("@ss.hasPermi('clzxzl:cgbg:add')")
    @Log(title = "测量专项资料成果报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClzxzlCgbg clzxzlCgbg) {
        clzxzlCgbg.setUserId(Math.toIntExact(getUserId()));
        clzxzlCgbg.setDeptId(Math.toIntExact(getDeptId()));
        clzxzlCgbg.setCreateBy(getUsername());

        return toAjax(clzxzlCgbgService.insertClzxzlCgbg(clzxzlCgbg));
    }

    /**
     * 修改测量专项资料成果报告
     */
    @PreAuthorize("@ss.hasPermi('clzxzl:cgbg:edit')")
    @Log(title = "测量专项资料成果报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClzxzlCgbg clzxzlCgbg) {
        return toAjax(clzxzlCgbgService.updateClzxzlCgbg(clzxzlCgbg));
    }

    /**
     * 删除测量专项资料成果报告
     */
    @PreAuthorize("@ss.hasPermi('clzxzl:cgbg:remove')")
    @Log(title = "测量专项资料成果报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clzxzlCgbgIds}")
    public AjaxResult remove(@PathVariable Integer[] clzxzlCgbgIds) {
        return toAjax(clzxzlCgbgService.deleteClzxzlCgbgByClzxzlCgbgIds(clzxzlCgbgIds));
    }
}
